// pages/userHobbies/userHobbies.js
const util = require('../../utils/util');
const API = require('../../common/constant');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    food: [],
    music: [],
    movies: [],
    book: [],
    place: [],
    sports: [],
    oldHobbies:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.getHobbies();
  },
  /**
   * 获取用户兴趣爱好标签
   */
  getHobbies: function(){
    var that = this;
    util.http({
      url: API.URL.USER.FIND_USERINFO,
      success: res => {
        console.log(res.data);
        this.setData({
          oldHobbies: res.data.bussData.hobby
        })
        this.changeHobby(res.data.bussData.hobby);
      },
      fail: rej => {
        console.log(rej);
      }
    })
  },
  /**
   * 修改hobby
   */
  changeHobby: function(data){
    let currentHobbies = data.split('[')[1].split(']')[0].split(',')[0].split('{');
    let temp,t ; // 临时变量
    let food = [], music = [], movies = [], book = [], place = [], sports =[];
    for(let i = 1 ; i< currentHobbies.length; i++){
      temp = currentHobbies[i].split(':');
      t = temp[1].split('"')[1].split('，');
      switch(temp[0]){
        case '"饮食"': food = t; break;
        case '"音乐"': music = t; break;
        case '"电影"': movies = t; break;
        case '"书"': book = t; break;
        case '"地方"': place = t; break;
        case '"运动"': sports = t; break;
      }
      t = [];
    }
    this.setData({
      food: food,
      music: music,
      movies: movies,
      book: book,
      place: place,
      sports: sports
    });
  },
  /**
   * 设置标签
   */
  setTags: function(name,tag){
    let data = this.data[name];
    console.log(name);
    console.log(tag[name]);
    console.log(data);
    if(data.length < 5){
      if(data.indexOf(tag[name]) == -1){
        data.push(tag[name]);
        let namestr = name+"Input";
        this.setData({
          [name]: data,
          [namestr]: ''
        });
      } else {
        app.showUserInfo('已经存在这个标签了','none',1500);
          this.setData({
            foodInput: ''
          });
      }
    } else {
      app.showUserInfo('最多添加5个标签','none',1500);
        this.setData({
          foodInput: ''
        });
    }
  },
  /**
   * 添加food标签
   */
  getInput: function(e){
    console.log(e);
    let newtag = e.detail.value;
    let name = JSON.stringify(newtag).split('"')[1];
    console.log(newtag);
    // console.log(newtag[name].length);
    if(newtag[name].length != 0 ){
      this.setTags(name,newtag);
    } else {
      app.showUserInfo('请输入标签内容','none',1500);
    }
  },
  /**
   * 长按删除
   */
  deleteTag: function(e){
    console.log(e);
    let name = e.target.dataset.name;
    let index = e.target.id;
    let newData = this.data[name];
    newData.splice(index,1);
    this.setData({
      [name]: newData
    });
  },
  saveTags: function(){
    let allInfo = [];
    let hobby = [{'food':"饮食"},{'music': "音乐"},{'movies':"电影"},{'book': "书"},{'place':"地方"},{'sports':"运动"}]
    for (let key in hobby) {
      let name = Object.keys(hobby[key])[0];
      let namestr = hobby[key][name];
      if(this.data[name].length!=0){
        allInfo.push({
          [namestr]: this.data[name].join('，')
        })
      }
    }
    // 将获得的数据转换为同一个格式
    let a = [];
    for(let key in allInfo){
      a.push(JSON.stringify(allInfo[key]));
    }
    let str = '['+ a.join('，') + ']';
    console.log(str);
    console.log(this.data.oldHobbies);
    // 判断 两组数据是否相等，相等就不更新资料
    if(str == this.data.oldHobbies){
      console.log('yiyiyiyi');
      app.showUserInfo('您没有添加新的爱好','none',1500);
    } else {
      console.log('buyiy');
      util.http({
        url: API.URL.USER.EDIT_MINIUSERINFO,
        data: {
          hobby: str
        },
        success: res => {
          console.log(res);
          app.showUserInfo('成功修改兴趣爱好','none',1500);
          setTimeout(()=>{
            wx.navigateBack()
            app.globalData.isNewMyInfo = true
          },1500);
          
        },
        fail: err => {
          console.log(err);
        }
      })
    }
    
    console.log(typeof(JSON.stringify(allInfo)));
  }
})